﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;

namespace Pap.Web.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public static class Extentions
    {
        // shortcut of the ServiceLocator.Current.Resolve
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewModel">The view model.</param>
        /// <returns></returns>
        public static T GetInstance<T>(this IViewModel viewModel)
        {
            return ServiceLocator.Current.GetInstance<T>();
        }
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static object GetInstance(this IViewModel viewModel, Type type)
        {
            return ServiceLocator.Current.GetInstance(type);
        }
        /// <summary>
        /// Gets all instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewModel">The view model.</param>
        /// <returns></returns>
        public static IEnumerable<T> GetAllInstance<T>(this IViewModel viewModel)
        {
            return ServiceLocator.Current.GetAllInstances<T>();
        }
        /// <summary>
        /// Gets all instance.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static IEnumerable<object> GetAllInstance(this IViewModel viewModel, Type type)
        {
            return ServiceLocator.Current.GetAllInstances(type);
        }


        /// <summary>
        /// Redirects the specified current page.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="module">The module.</param>
        /// <param name="view">The view.</param>
        /// <param name="querystrings">The querystrings.</param>
        public static void Redirect(this Control currentPage, string module, string view, params string[] querystrings)
        {
            currentPage.Page.Response.Redirect(ResolveUrl(currentPage, module + ".route", view, querystrings));
        }
        /// <summary>
        /// Redirects the specified current page.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="view">The view.</param>
        /// <param name="querystrings">The querystrings.</param>
        public static void Redirect(this Control currentPage, string view, params string[] querystrings)
        {
            currentPage.Redirect(get_Module(currentPage.Page), view, querystrings);
        }
        /// <summary>
        /// Redirects from this page.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="Module">The module.</param>
        /// <param name="View">The view.</param>
        /// <param name="querystrings">The querystrings.</param>
        public static void RedirectFromThisPage(this Control currentPage, string Module, string View, params string[] querystrings)
        {
            List<string> QueryString = new List<string>(querystrings);
            QueryString.Add("PrevPage=" + currentPage.Page.Request.RawUrl);
            currentPage.Redirect(Module, View, QueryString.ToArray());
        }
        /// <summary>
        /// Redirects from this page.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="view">The view.</param>
        /// <param name="querystrings">The querystrings.</param>
        public static void RedirectFromThisPage(this Control currentPage, string view, params string[] querystrings)
        {
            currentPage.RedirectFromThisPage(get_Module(currentPage.Page), view, querystrings);
        }
        /// <summary>
        /// Redirects to previous page.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        public static void RedirectToPreviousPage(this Control currentPage)
        {
            string prevPage = HttpContext.Current.Request.QueryString["PrevPage"];
            if (string.IsNullOrEmpty(prevPage))
                prevPage = "~/";
            HttpContext.Current.Response.Redirect(prevPage);
        }

        /// <summary>
        /// Resolves the URL.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="module">The module.</param>
        /// <param name="view">The view.</param>
        /// <param name="querystrings">The querystrings.</param>
        /// <returns></returns>
        public static string ResolveUrl(this Control currentPage, string module, string view, params string[] querystrings)
        {
            string QueryString, Url;

            Url = string.Format(UrlTemplates.ViewsUrl, module, view);
            QueryString = string.Join("&", querystrings).Trim();

            if (!string.IsNullOrEmpty(QueryString))
                Url += "?" + QueryString;
            return Url;
        }
        /// <summary>
        /// Resolves the URL.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="view">The view.</param>
        /// <param name="querystrings">The querystrings.</param>
        /// <returns></returns>
        public static string ResolveUrl(this Control currentPage, string view, params string[] querystrings)
        {
            return ResolveUrl(currentPage, get_Module(currentPage.Page), view, querystrings);
        }


        /// <summary>
        /// Get_s the module.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public static string get_Module(this Control currentPage)
        {
            return HttpContext.Current.Items["__Module"] as string;
        }
        /// <summary>
        /// Set_s the module.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="value">The value.</param>
        public static void set_Module(this Control currentPage, string value)
        {
            HttpContext.Current.Items["__Module"] = value;
        }
        /// <summary>
        /// Get_s the view.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public static string get_View(this Control currentPage)
        {
            return HttpContext.Current.Items["__View"] as string;
        }
        /// <summary>
        /// Set_s the view.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="value">The value.</param>
        public static void set_View(this Control currentPage, string value)
        {
            HttpContext.Current.Items["__View"] = value;
        }


        /// <summary>
        /// Adds the image.
        /// </summary>
        /// <param name="images">The images.</param>
        /// <param name="imageData">The image data.</param>
        /// <param name="labels">The labels.</param>
        /// <param name="typeId">The type id.</param>
        /// <returns></returns>
        public static IImage AddImage(this IEnumerable<IImage> images, byte[] imageData, string labels, Guid typeId)
        {
            var inMemoryImage = new InMemoryImage();
            inMemoryImage.ImageData = imageData;
            inMemoryImage.ImageID = Guid.NewGuid();
            inMemoryImage.Label = labels;
            inMemoryImage.ImageTypeID = typeId;

            ((IList)images).Add(inMemoryImage);
            return inMemoryImage;
        }


        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static object GetProperty(this object obj, string propertyName)
        {
            return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
        }
        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static T GetProperty<T>(this object obj, string propertyName)
        {
            return (T)GetProperty(obj, propertyName);
        }

        /// <summary>
        /// Gets or sets the process date.
        /// </summary>
        /// <value>
        /// The process date.
        /// </value>
        public static DateTime ProcessDate
        {
            get
            {
                if (HttpContext.Current.Application["ProcessDate"] == null)
                    HttpContext.Current.Application["ProcessDate"] = DateTime.Now;

                DateTime proDate = Convert.ToDateTime(HttpContext.Current.Application["ProcessDate"]);

                if (proDate.Date < DateTime.Today)
                {
                    var adminRepository = ServiceLocator.Current.GetInstance<IAdministrationRepository>();
                    string automaticCloseHuor = adminRepository.GetSettingValue(RegistryInfo.CommitteeID, SettingKeys.AutomaticCloseDay);
                    if (!automaticCloseHuor.IsNullOrEmpty())
                    {
                        if (DateTime.Now.Hour >= automaticCloseHuor.ToInt32())
                            HttpContext.Current.Application["ProcessDate"] = DateTime.Now;
                    }
                }

                return Convert.ToDateTime(HttpContext.Current.Application["ProcessDate"]);
            }

            set
            {
                HttpContext.Current.Application["ProcessDate"] = value;
            }
        }
    }
}
